package com.goodboys.service.impl;

import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.service.impl.BaseServiceImpl;
import com.goodboys.dao.InfoDao;
import com.goodboys.entity.Comment;
import com.goodboys.entity.Info;
import com.goodboys.entity.User;
import com.goodboys.service.InfoService;
import com.goodboys.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by CJ on 2018-05-19.
 */
@Service("infoService")
public class InfoServiceImpl extends BaseServiceImpl<Info> implements InfoService {

    private final Logger    logger = LoggerFactory.getLogger(InfoServiceImpl.class);

    @Override
    public Map infos(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap();map.put("status", 0);
        if (baseReqBean != null) {
            //获取附近信息服务
            Double latRange = baseReqBean.getRange()*0.0001;
            Double lngRange = (Double) (baseReqBean.getRange()*0.0001) / 1.1;
            map.put("latRange", latRange);
            map.put("lngRange", lngRange);
            map.put("lat", baseReqBean.getLat());
            map.put("lng", baseReqBean.getLng());
            map.put("userId", baseReqBean.getId());
            map.put("page", baseReqBean.getPage()-1);
            map.put("tag", baseReqBean.getTag());
            map.put("type", baseReqBean.getType());
            map.put("size", baseReqBean.getSize());
            map.put("keyword", baseReqBean.getKeyword());
            List<Info> infos = infoDao.infoList(map);
            Integer total = infoDao.infoListCount(map);
            map.put("list", infos);
            map.put("total", (total == null) ? 0 : total);
            map.put("status", 1);
        }else {
            map.put("message", "参数存在为空，请重试。。。");
            logger.error("ERROR=====>userInfos:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }


    /**
     * 用户发布信息
     * @param baseReqBean
     * @return
     * @throws Exception
     */
    @Override
    public Map publish(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap();map.put("status", 0);
        if (baseReqBean != null && baseReqBean.getId() != null &&
                baseReqBean.getTitle() != null && baseReqBean.getContent() != null &&
                baseReqBean.getType() != null && baseReqBean.getTag() != null) {
            Info info = convertReqBeanToInfo(baseReqBean);
            //发布信息后显示最新的信息
            //根据userId查询user详情
            User user = userDao.selectByPrimaryId(baseReqBean.getId());
            info.setUser(user);
            infoDao.insertSelective(info);
            map.put("status", 1);
            map.put("message", info);
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>publish:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }

    //根据info的id查询该信息详情，包含comment
    public List<Info> findInfoDetail(BaseReqBean baseReqBean) throws  Exception{
        List<Info> infos = new ArrayList<>();
        infos= infoDao.findInfoDetail(baseReqBean.getId());
        return infos;
    }



    /**
     * 将baseReqBean转换为Info对象
     * @param baseReqBean
     * @return
     */
    public static Info convertReqBeanToInfo(BaseReqBean baseReqBean){
        Info info= new Info();
        info.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        info.setUserId(baseReqBean.getId());
        info.setTitle(baseReqBean.getTitle());
        info.setContent(baseReqBean.getContent());
        info.setType(baseReqBean.getType());
        info.setPublicTime(new Date());
        info.setTag(baseReqBean.getTag());
        info.setLng(baseReqBean.getLng());
        info.setLat(baseReqBean.getLat());
        return  info;
    }

}
